﻿using System;
using System.Collections.Generic;
using System.Web;
using FTchina.Spider;
using System.Collections;
using System.Text.RegularExpressions;
using System.Data;
namespace FTchina.RSS
{
    /// <summary>
    ///Getlist 的摘要说明
    /// </summary>
    public class Getlist
    {
        public Getlist()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }


        public void start(string url, string guizeid, string lanmuid, string regkuai, string reghref)
        {
             string kuai = GetListHtml(url, regkuai);
            ArrayList hrefs = GetAllHrefs(url, kuai, reghref);
            ArrayList sqls = new ArrayList();
            foreach (string href in hrefs)
            {
                sqls.Add("insert into cjurllist(url,rssguize,lanmuid) values ('" + href + "','" + guizeid + "','" + lanmuid + "')");
            }
            SaveList2DB(sqls);
        }

        /// <summary>
        /// 获取包含超链接列表的html代码块
        /// </summary>
        /// <param name="url">绝对地址</param>
        /// <param name="reg">正则取代码块</param>
        /// <returns></returns>
        protected string GetListHtml(string url, string reg)
        {
            GetHtml htm = new GetHtml();
            string tmpHtml = htm.GetPage(url);

            tmpHtml = htm.ExecRegWithHref(tmpHtml, reg);
             return tmpHtml;
        }




        /// <summary>
        /// 获取所有的超级链接
        /// </summary>
        /// <param name="url">参考地址，http://www.weibaohui.com/baidu.html</param>
        /// <param name="html">html代码块</param>
        /// <param name="reghref">获取href的正则</param>
        /// <returns>所有超链接的集合</returns>
        protected ArrayList GetAllHrefs(string url, string html, string reghref)
        {
            ArrayList my_list = new ArrayList();

            Regex re = new Regex(reghref, RegexOptions.IgnoreCase);
            MatchCollection mc = re.Matches(html);

            for (int i = 0; i <= mc.Count - 1; i++)
            {
                bool _foo = false;
                string name = mc[i].Groups[1].Value.ToString();
                foreach (string list in my_list)
                {
                    if (name == list || name.Contains("#") || name.Contains("Comment.asp?ArticleID") || name.ToLower().StartsWith("javascript:"))
                    {
                        _foo = true;
                        break;
                    }
                }//过滤重复连接

                if (!_foo)
                {
                    name = basic.GetFullUrl(url, name);
                    my_list.Add(name);
                }
            }
            return my_list;
        }

        /// <summary>
        /// 将获取到得连接存入数据库
        /// </summary>
        /// <param name="SqlsList">插入数据库的sql语句</param>
        /// <returns></returns>
        protected Boolean SaveList2DB(ArrayList SqlsList)
        {
            return shujuku.ExecuteSqlTransaction(SqlsList);

        }

        /// <summary>
        /// 获取规则信息然后采集
        /// </summary>
        /// 
       [STAThread]
        public static void PregetguizeAndCj()
        {
            DataTable dt = shujuku.GetDataTable("select * from cjurllist where ispass=0 order by id desc");
            string regcontent, regsource; 
           foreach (DataRow dr in dt.Rows)
            {
                regcontent = shujuku.GetValueByKey("content", "rssguize", "id='" + dr["rssguize"] + "'", "regcontent" + dr["rssguize"].ToString());
                regsource = shujuku.GetValueByKey("source", "rssguize", "id='" + dr["rssguize"] + "'", "regsource" + dr["rssguize"].ToString());
                SaveArt2DB(dr["url"].ToString(), dr["lanmuid"].ToString(), regcontent, regsource);
            }

        }
       [STAThread]

       static void SaveArt2DB(string url, string lanmu, string regcontent, string regsource)
        {
            try
            {
                string dbcontent = string.Empty;
                string dbsource = string.Empty;

                GetHtml htm = new GetHtml();
                string tmpHtml = htm.GetPage(url);
                string title = htm.GetTitle(tmpHtml).Replace("-生活小窍门网","");
                dbcontent = basic.SafeRequest(htm.ExecReg(tmpHtml, regcontent));
                dbcontent = basic.DealScript(dbcontent);
                dbsource = basic.SafeRequest(htm.ExecReg(tmpHtml, regsource));
                dbcontent += "<br/><br/><a href=" + url + " target=_blank>[网友投稿]</a>";


                string sql = "insert into content (lanmuid,title,content,author,source,ispass) values ('" + lanmu + "','" + title + "','" + dbcontent + "','" + dbsource + "','" + dbsource + "','1')";
                string RemoveListSql = "update cjurllist set ispass='1' where url='" + url + "'";

                if (dbcontent.Trim().Length != 0 && title != "标题未填写")
                {
                    shujuku.ExecuteSql(sql);
                }
                shujuku.ExecuteSql(RemoveListSql);
                if (basic.IsHtml) FTchina.Api.htmlpath.MakeNewHtmlByBiggestID();
            }
            catch { }
        }

    }
}